package com.xinghen.amqp.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xinghen
 * @ClassName: Rabbitmq
 * @Package: com.xinghen.amqp.rabbitmq
 * @Description Rabbitmq
 * @create 2017-12-08 15:36
 */
@Component
public class Rabbitmq {

    /**
     * 应用名称
     */
    @Value("${spring.application.name}")
    private String springApplicationName;

    @Autowired
    private AmqpTemplate amqpTemplate;


    /**
     * Convert a Java object to an Amqp Message and send it to a specific exchange with a
     * specific routing key.
     *
     * @param exchange   exchange
     * @param routingKey routingKey
     * @param message    a message to send
     * @param <T>        任意类型
     */
    public <T> void convertAndSend(String exchange, String routingKey, RabbitmqMessage<T> message) {
        message.setSender(springApplicationName);
        message.setSendDate(new Date());
        message.setExchange(exchange);
        message.setRoutingKey(routingKey);
        message.setTimestamp(System.currentTimeMillis());
        this.amqpTemplate.convertAndSend(exchange, routingKey, message);
    }

}
